package work.atpgroup.service.impl;

import org.springframework.stereotype.Service;
import work.atpgroup.entity.Identity;
import work.atpgroup.entity.User;
import work.atpgroup.mapper.IdentityMapper;
import work.atpgroup.mapper.UserMapper;
import work.atpgroup.mapper.union.UserIdentityMapper;
import work.atpgroup.service.UserService;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource(name="identityMapper")
    private IdentityMapper identityMapper;
    @Resource(name="userMapper")
    private UserMapper userMapper;
    @Resource(name="userIdentityMapper")
    private UserIdentityMapper userIdentityMapper;
    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(Integer userId) {
        return userMapper.deleteUser(userId);
    }

    @Override
    public User getUserByComplexNameOrId(String complexNameOrId) {
        User user = userMapper.getUserByComplexNameOrId(complexNameOrId);
        if(user!=null){
            loadIdentity(user);
        }
        return user;
    }

    @Override
    public List<User> getUserByListId(List<Integer> id) {
        return userMapper.getUserByListId(id);
    }

    @Override
    public int login(@Nullable String complexNameOrId,@Nullable Long password) {
        if(complexNameOrId==null||password==null){
            return -1;
        }
        User user = getUserByComplexNameOrId(complexNameOrId);
        if(user==null){
            return -1;
        }
        if(!Objects.equals(user.getPassword(), password)){
            return -1;
        }
        if(Objects.equals(user.getUsername(), complexNameOrId)){
            return 1;
        }
        if(Objects.equals(user.getNickname(), complexNameOrId)){
            return 1;
        }
        if(Objects.equals(user.getSchoolId(), complexNameOrId)){
            return 1;
        }
        if(Objects.equals(user.getCollegeId(), complexNameOrId)){
            return 1;
        }
        return -1;
    }

    @Override
    public void loadIdentity(User user) {
        ArrayList<Integer> identity_ids = userIdentityMapper.getEveryIdentityIdOfThisUser(user.getUserId());
        user.getIdentities().clear();
        for(Integer identity_id : identity_ids){
            Identity identity = identityMapper.getIdentityById(identity_id);
            user.getIdentities().add(identity);
        }
    }

    @Override
    public User getUserByUserId(Integer userId) {
        return userMapper.getUserByUserId(userId);
    }

    @Override
    public int getUserCountByComplexName(String complexName) {
        return userMapper.getUserCountByComplexName(complexName);
    }

    @Override
    public boolean haveAdministratorPermission(Integer userId) {
        return havePermission(userId,"管理员");
    }

    @Override
    public boolean havePermission(Integer userId, String identityName) {
        Identity identity = identityMapper.getIdentityByName(identityName);
        return havePermission(userId,identity);
    }

    @Override
    public boolean havePermission(Integer userId, Identity identity) {
        return havePermission(userId,identity.getIdentityId());
    }

    @Override
    public boolean havePermission(Integer userId, Integer identityId) {
        return userIdentityMapper.getEveryUserIdWithThisIdentity(identityId).contains(userId);
    }
}
